<script setup>
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
const isRegister = ref(true)

const registerData = ref({
  username: '',
  pwd: '',
  rePwd: ''
})

const clearData = () => {
  registerData.value.username = ''
  registerData.value.pwd = ''
  registerData.value.rePwd = ''
}

// 表单校验规则
const validateUsername = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入账号'))
  } else if (value.length < 3 || value.length > 15) {
    callback(new Error('账号长度必须在3-15个字符之间'))
  } else {
    callback()
  }
}

const validatePassword = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入密码'))
  } else if (value.length < 8 || value.length > 12) {
    callback(new Error('密码长度必须在8-12个字符之间'))
  } else {
    callback()
  }
}

const validateRePassword = (rule, value, callback) => {
  if (isRegister.value) {
    if (!value) {
      callback(new Error('请再次输入密码'))
    } else if (value !== registerData.value.pwd) {
      callback(new Error('两次输入的密码不一致'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}



// 表单校验规则对象
const formRules = {
  username: [
    { validator: validateUsername, trigger: 'blur' }
  ],
  pwd: [
    { validator: validatePassword, trigger: 'blur' }
  ],
  rePwd: [
    { validator: validateRePassword, trigger: 'blur' }
  ]
}
// 表单引用
const formRef = ref()
const register = async () => {
  await formRef.value.validate()
  // await userRegisterService(registerData.value.username, registerData.value.pwd);
}

// watch(registerData, (newVal) => {
//   console.log(newVal)
// }, {deep: true})

</script>

<template>
  <div class="login-container">
    <div class="login-form" v-if="isRegister">
      <el-form ref="formRef" :model="registerData" :rules="formRules">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="registerData.username" :prefix-icon="User" style="width: 300px"
                    placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input
            v-model="registerData.pwd" style="width: 300px"
            type="password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item prop="rePwd">
          <el-input
            v-model="registerData.rePwd" style="width: 300px"
            type="password"
            :prefix-icon="Lock"
            placeholder="请再次输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 300px" @click="register()">注册</el-button>
        </el-form-item>
        <el-form-item>
          <el-link type="info" underline="never" @click="isRegister=false; clearData()">
            ← 登录
          </el-link>
        </el-form-item>
      </el-form>
    </div>


    <div class="login-form" v-if="!isRegister">
      <el-form ref="formRef" :model="registerData" :rules="formRules">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="registerData.username" style="width: 300px"
                    :prefix-icon="User"
                    placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input
            v-model="registerData.pwd" style="width: 300px"
            type="password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 300px">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-link type="info" underline="never" @click="isRegister=true; clearData()">
            ← 注册
          </el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url(../../assets/background.webp);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-form {
  background-color: rgba(255, 255, 255, 0.7);
  padding: 30px 40px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  text-align: center;
  backdrop-filter: blur(10px);
}

.login-form h1 {
  margin-bottom: 30px;
  color: #333;
}
</style>
